import axios from 'axios'
// import { MessageBox, Message } from 'element-ui'
// import store from '@/store'
// import {
//   getToken
// } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  //baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // baseURL: process.env.NODE_ENV == "development" ? process.env.VUE_APP_BASE_API : process.env.VUE_APP_BASE_URL +
  //   process
  //   .env.VUE_APP_BASE_API,
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 50000 // request timeout
})
service.interceptors.request.use(config => {
  // Do something before request is sent
  config.headers['Content-Type'] = 'text/plain'
  return config;
}, error => {
  // Do something with request error
  console.log(error); // for debug
  Promise.reject(error);
})




// request interceptor
// request拦截器
// service.interceptors.request.use(config => {
//   // Do something before request is sent
//   if (store.getters.token) {
//     config.headers['Authorization'] = getToken(); // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
//   }
//   if (store.getters.language) {
//     config.headers['lang'] = store.getters.language;
//   } else {
//     config.headers['lang'] = 'zh_CN';
//   }
//   return config;
// }, error => {
//   // Do something with request error
//   console.log(error); // for debug
//   Promise.reject(error);
// })

// response interceptor
// respone拦截器
service.interceptors.response.use(
  response => {
    /**
     * 下面的注释为通过response自定义code来标示请求状态，当code返回如下情况为权限有问题，登出并返回到登录页
     * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
     */
    const res = response.data;
    // console.log("res", res)
    if (response.status === 401 || res.status === 40101) {

      return Promise.reject('error');
    }


    if (response.status !== 200 && res.status !== 200) {
      // Message({
      //   message: res.message,
      //   type: 'error',
      //   duration: 5 * 1000
      // });
    } else {
      return response.data;
    }
  },
  error => {
    console.log(error); // for debug

    return Promise.reject(error);
  }
);

export default service